package com.yz.common.utils;

import com.yz.common.constant.Constants;

import java.security.SecureRandom;

/**
 * Created With IntelliJ IDEA.
 * Descriptions:验证码工具类
 * <p>
 * User:Mr.Du
 * Date:2024/3/25
 * Time:16:33
 */
public class PromotionCodeUtils {

    // 优惠码字符集，这里使用数字和字母
    private static String charset = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";

    public static String genPromotionCode() {
        StringBuilder sb = new StringBuilder();
        // 创建一个安全的随机数生成器
        SecureRandom random = new SecureRandom();
        // 生成指定长度的优惠码
        for (int i = 0; i < Constants.PROMOTION_LENGTH; i++) {
            // 从字符集中随机选择一个字符并添加到优惠码中
            int randomIndex = random.nextInt(charset.length());
            sb.append(charset.charAt(randomIndex));
        }
        return sb.toString();
    }

    public static String genToken() {
        StringBuilder sb = new StringBuilder();
        // 创建一个安全的随机数生成器
        SecureRandom random = new SecureRandom();
        for (int i = 0; i < Constants.PROMOTION_TOKEN_LENGTH; i++) {
            int randomIndex = random.nextInt(charset.length());
            sb.append(charset.charAt(randomIndex));
        }
        return sb.toString();
    }

    public static String[] getPromotionInfo(String code) {
        String[] str = new String[3];
        int x = Constants.PROMOTION_LENGTH, y = Constants.PROMOTION_TOKEN_LENGTH;
        str[0] = code.substring(0, x);
        str[1] = code.substring(x, x + y);
        str[2] = code.substring(x + y);
        return str;
    }



}
